//
//  TVCardUnlinkView.swift
//  StarTimesON
//
//  Created by Toj on 8/24/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit
import YYText

class TVCardUnlinkView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 背景图片
    private lazy var backgroundView: UIImageView = {
        let view = UIImageView()
        view.isUserInteractionEnabled = true
        view.image = "commodity_tv_card_bgImg_not".to_image
        return view
    }()
    
    // link
    private lazy var linkButton: UIButton = {
        let view = UIButton()
        view.addTarget(self, action: #selector(didTapLinkCard), for: .touchUpInside)
        view.setImage("commodity_tv_card_link".to_image, for: .normal)
        return view
    }()
    
    // 标题
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.textAlignment = .center
        view.text = "membership_TV_LINK".localized()
        view.textColor = "#E8CD8C".to_color
        view.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
        return view
    }()
    
    // 提示
    private lazy var tipsLabel: YYLabel  = {
        let style = NSMutableParagraphStyle()
        style.lineBreakMode = .byTruncatingMiddle
        style.alignment = .center
        let font = UIFont.systemFont(ofSize: 12.f_scale)
        
        let text = "dvb_activation_notice".localized()
        let highText = text.replacingOccurrences(of: ".", with: "").split(separator: Character.init(" ")).last ?? ""
        let (attributedText, range) = text.to_attributedString(font: font, color: "#999999".to_color, paragraph: style, highlight: String(highText), hcolor: "#0087EB".to_color, paragraph: style, hlineStyle: .underline)
        attributedText.yy_setTextHighlight(range, color: nil, backgroundColor: nil) { (_, _, _, _) in
            if !DataCenter.isSupportFAQ(entrance: FAQEntranceName.dvblink_main_anonymous) { return }
            let event = DSEvent(category: "dvbservice", action: "linkpage_here_click")
            DataStatistics.sendEvent(event: event)
            STSRouter.openFAQPage(FAQEntranceName.dvblink_main_anonymous)
        }
        let view = YYLabel()
        view.numberOfLines = 0
        view.attributedText = attributedText
        return view
    }()
    
    @objc private func didTapLinkCard() {
        if STUserModel.shared.isLogin() {
            STUserModel.shared.loginTo(targetUrl:RouterPage.dvbLink.routerPath)
            return
        }
        STUserModel.shared.loginTo(targetUrl: "")
    }
}

// MARK: - 初始化
extension TVCardUnlinkView {
    
    func setupMakeAddSubviews() {
           
        addSubview(backgroundView)
        backgroundView.addSubview(linkButton)
        backgroundView.addSubview(titleLabel)
        addSubview(tipsLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        backgroundView.snp.makeConstraints { make in
            make.top.left.right.equalTo(self)
            make.height.equalTo(120.ui_scale)
        }
        
        linkButton.snp.makeConstraints { make in
            make.top.equalTo(22.ui_scale)
            make.centerX.equalTo(backgroundView)
            make.width.equalTo(48.ui_scale)
            make.height.equalTo(48.ui_scale)
        }
        
        // 标题
        titleLabel.snp.makeConstraints { make in
            make.bottom.left.right.equalTo(backgroundView)
            make.top.equalTo(linkButton.snp.bottom)
        }
        
        tipsLabel.snp.makeConstraints { make in
            make.top.equalTo(backgroundView.snp.bottom).offset(8.ui_scale)
            make.left.right.bottom.equalTo(self)
        }
    }
}
